﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lion.WeiXin.Core.Models;
using Lion.WeiXin.Core.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Lion.WeiXin.Tools.Log;
namespace Lion.WeiXin.Controllers
{
      [PermissionFilter]
    public class RoleController : Controller
    {
        IRole irole = new RoleDAO();

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///  获取分页列表数据
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="msgType"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public string GetPageList(string roleName, int currentPage, int pageSize)
        {
            //数据量少的，直接查全部数据分页展示           
            List<RoleModel> list = irole.GetList(string.Empty);
            int totalCount = list.Count;
            //如果传入条件参数
            if (!string.IsNullOrEmpty(roleName))
            {
                list = list.Where(n => n.RoleName.Contains(roleName)).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
            }
            else
            {
                list = list.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
            }

            string jsonResult = JsonConvert.SerializeObject(list, new IsoDateTimeConverter());
            jsonResult = "{\"total\":" + totalCount + "," + "\"data\":" + jsonResult + "}";
            return jsonResult;
        }

        public ActionResult Show()
        {
            string ID = Request.QueryString["ID"];
            if (string.IsNullOrEmpty(ID))
            {
                return Redirect(Request.RawUrl);
            }
            int roleID = int.Parse(ID);
            RoleModel model = irole.GetModel(roleID);  

            //获取当前角色拥有的所有菜单
            ICmsMenu imenu = new CmsMenuDAO();
            model.Menus= imenu.GetRoleHaveMenus(roleID).Where(m=>m.Code.Length>=6&&m.Code.Contains("100")).ToList();
            //只保留 二级菜单
            return View(model);
        }

        public ActionResult Create()
        {
            return View();
        }


        public ActionResult Save(FormCollection form)
        {
            string action = Request.QueryString["Action"];
            string result = string.Empty;

            RoleModel model = new RoleModel();
            TryUpdateModel(model);
            result = SaveStateEnum.OK.ToString();
            try
            {
                if (action == ActionEnum.Create.ToString())
                {
                    irole.Add(model);
                }
                else if (action == ActionEnum.Update.ToString())
                {
                    irole.Update(model);
                }
                else
                {
                    result = SaveStateEnum.NO.ToString();
                }

            }
            catch (Exception ex)
            {
                LogHelper.Error(ex, "数据{0}失败", action);
                result = SaveStateEnum.NO.ToString();
            }

            return Content(result);
        }

        public ActionResult Edit(int roleID, string roleName,string remark, string checkMenuIDs)
        {
            string result = SaveStateEnum.OK.ToString();
            RoleModel model = new RoleModel();
            model.AID = roleID;
            model.RoleName = roleName;
            model.Remark = remark;
            if (!string.IsNullOrEmpty(checkMenuIDs))
            {
                foreach (string item in checkMenuIDs.Split(';'))
                {
                    if (!string.IsNullOrEmpty(item)&&item!=";")
                    {
                        CmsMenuModel menuModel = new CmsMenuModel();
                        menuModel.AID = int.Parse(item);
                        model.Menus.Add(menuModel);
                    }
                }
            }
            try
            { 
                irole.Update(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex, "数据修改失败");
                result = SaveStateEnum.NO.ToString();
            }

            return Content(result);
        }
   

        public ActionResult Delete(string IDs)
        {
            string result = SaveStateEnum.NO.ToString();
            try
            {
                if (!string.IsNullOrEmpty(IDs))
                {
                    IDs = IDs.TrimEnd(',');
                    irole.DeleteList(IDs);
                    result = SaveStateEnum.OK.ToString();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex, "删除数据失败!");
            }
            return Content(result);
        }

        //获取所有菜单列表，然后按等级显示，加载当前角色所有菜单，设置选中
    }
}
